package com.atguigu.guli.service.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.vod.properties.VodProperties;
import com.atguigu.guli.service.vod.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

@Slf4j
@Service
public class MediaServiceImpl implements MediaService {

    @Autowired
    private VodProperties vodProperties;

    // 上传视频
    @Override
    public String upload(MultipartFile file) {
        try {
            // 获取文件名称
            String filename = file.getOriginalFilename();
            // 获取文件流
            InputStream is = file.getInputStream();
            //配置请求参数
            UploadStreamRequest request = new UploadStreamRequest(vodProperties.getAccessKeyId(),
                    vodProperties.getAccessKeySecret(),
                    filename, filename, is);
            //工作流id: 审核 加密转码
            request.setWorkflowId(vodProperties.getWorkflowId());
            //上传 客户端对象
            UploadVideoImpl uploader = new UploadVideoImpl();
            // 执行上传请求
            UploadStreamResponse response = uploader.uploadStream(request);
            if(response.isSuccess()){
                System.out.println("上传成功: "+ response.getVideoId());
                return response.getVideoId();
            }else{
                log.error("VideoId=" + response.getVideoId() + "\n");
                log.error("ErrorCode=" + response.getCode() + "\n");
                log.error("ErrorMessage=" + response.getMessage() + "\n");
                throw new GuliException(ResultCodeEnum.VIDEO_UPLOAD_ALIYUN_ERROR);
            }
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
            throw new GuliException(ResultCodeEnum.VIDEO_UPLOAD_ALIYUN_ERROR);
        }
    }

    // 获取未加密视频播放地址
    @Override
    public String getPlay(String vId) {
        try {
            // 获取配置文件中的地区id, keyId, keySecret
            DefaultProfile profile = DefaultProfile.getProfile(vodProperties.getRegionId(),
                    vodProperties.getAccessKeyId(), vodProperties.getAccessKeySecret());
            DefaultAcsClient client = new DefaultAcsClient(profile);
            GetPlayInfoResponse response;
            GetPlayInfoRequest request = new GetPlayInfoRequest();
            request.setVideoId(vId); //需要配置 要获取播放地址的视频的id
            response = client.getAcsResponse(request);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            String playUrl = null;
            //播放地址
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                playUrl = playInfo.getPlayURL();
            }
            return playUrl;  // 返回播放地址
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
            throw new GuliException(ResultCodeEnum.FETCH_VIDEO_UPLOADAUTH_ERROR);
        }
    }

    //获取加密视频播放凭证
    @Override
    public String getPlayAuth(String vId) {
        try {
            DefaultProfile profile = DefaultProfile.getProfile(vodProperties.getRegionId(),
                    vodProperties.getAccessKeyId(), vodProperties.getAccessKeySecret());
            DefaultAcsClient client = new DefaultAcsClient(profile);
            GetVideoPlayAuthResponse response;
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(vId);  // 获取播放地址的视频的id
            request.setAuthInfoTimeout(vodProperties.getExpireTime()); //过期时间 默认100秒，单位秒
            response = client.getAcsResponse(request);
            //返回播放凭证
            return response.getPlayAuth();
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
            throw new GuliException(ResultCodeEnum.FETCH_VIDEO_UPLOADAUTH_ERROR);
        }
    }
}
